package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdImageMapper;
import com.bwie.mapper.TbProdInfoMapper;
import com.bwie.mapper.TbUnitMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdImage;
import com.bwie.pojo.TbProdInfo;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.service.TbUnitService;
import com.bwie.utils.Result;
import com.bwie.vo.AttaVo;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.ProdPageVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author wx200
 * @description 针对表【tb_prod】的数据库操作Service实现
 * @createDate 2025-05-27 09:09:00
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
        implements TbProdService {
    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    HttpServletRequest request;

    @Autowired
    TbProdImageMapper tbProdImageMapper;

    @Autowired
    TbProdInfoMapper tbProdInfoMapper;

    @Override
    public Result getProdList(ProdPageVo prodPageVo) {
//        构造分页参数
        Page<TbProd> page = new Page<>(prodPageVo.getPageNum(), prodPageVo.getPageSize());
////      分页查询
//        QueryWrapper<TbProd> wrapper = new QueryWrapper<>();
//        Page<TbProd> tbProdPage = tbProdMapper.selectPage(page, wrapper);
        Page<TbProd> tbProdPage = tbProdMapper.selectProdList(page, prodPageVo);
        return Result.ok().setData(tbProdPage);
    }

    @Override
    public Result addProd(TbProd tbProd) {
//        输入商品spu信息时 需对spu名称做重复 必填的校验
        TbProd prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdSpu, tbProd.getProdSpu()));
        if (prod != null) {
            return Result.error().setMsg("spu商品已经存在");
        }
//       商品名称重复校验
        prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName, tbProd.getProdName()));
        if (prod != null) {
            return Result.error().setMsg("同名商品已存在");
        }
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);
        tbProd.setProdStatus(0);
        tbProd.setCreateBy(nickName);
        tbProd.setCreateTime(new Date());
        //       添加商品
        tbProdMapper.insert(tbProd);
        return Result.ok();
    }

    @Override
    public Result getTypeSelectTree() {
        List<TbProd> tbProds = tbProdMapper.selectList(null);
        return Result.ok().setData(tbProds);
    }

    @Override
    public Result deleteAllProd(List<TbProd> tbProdList) {
        for (TbProd tbProd : tbProdList) {
            tbProdMapper.deleteById(tbProd.getProdId());
        }
        return Result.ok();
    }

    @Override
    public Result addProdAtta(AttaVo attaVo) {
//        确认商品是否存在
        TbProd tbProd = tbProdMapper.selectById(attaVo.getProdId());
       if(tbProd==null){
           return Result.error().setMsg("商品不存在");
       }
//       更新商品信息
        tbProd.setProdVideo(attaVo.getProdVideo());
        tbProd.setUpType(attaVo.getUpType());
        tbProd.setUpTime(attaVo.getUpTime());
        tbProd.setDownType(attaVo.getDownType());
        tbProd.setDownTime(attaVo.getDownTime());
        tbProdMapper.updateById(tbProd);

//        添加商品轮播图
        if(attaVo.getImageList()!=null&&attaVo.getImageList().size()>0){
            for (String imageUrl : attaVo.getImageList()) {
                TbProdImage tbProdImage = new TbProdImage();
                tbProdImage.setProdId(attaVo.getProdId());
                tbProdImage.setImageUrl(imageUrl);
                tbProdImageMapper.insert(tbProdImage);
            }
        }
        return Result.ok();
    }

    @Override
    public Result getProdInfoList(PageInfoVo pageInfoVo) {
        //        构造分页参数
        Page<TbProd> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
//        2.获取品牌列表（分页列表
        QueryWrapper<TbProdInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbProdInfo::getProdId,pageInfoVo.getTypeId());
        wrapper.lambda().orderByDesc(TbProdInfo::getInfoId);

       Page<TbProdInfo> tbProdInfoPage= tbProdImageMapper.getProdInfoList(page,pageInfoVo.getTypeId());

//       初始化购买数量
        for (TbProdInfo record : tbProdInfoPage.getRecords()) {
            record.setBuyNum(0);
        }

        return Result.ok().setData(tbProdInfoPage);
    }


}




